<?php
if (!(defined('IN_IA')))
{
    exit('Access Denied');
}
/**
 * User: 邓志锋 <280160522@qq.com> <http://www.diygw.com>
 * Date: 2018-07-28
 * Time: 下午 6:06
 */
class Cart_Diygwcom extends Interceptor_Diygwcom
{
    public function index()
    {
        global $_GPC, $_W;
        $dashboardid = $_GPC["dashboardid"];
        $extendValues = $this->getDashboardExtends($dashboardid);
        $title="我的购物车";
        include $this->template();
    }

    public function save()
    {
        global $_GPC, $_W;
        $dashboardid = safe_gpc_int($_GPC["dashboardid"]);
        $uid =$this->getUid();

        $tablename=$this->getTable();
        $linkjson['tablename']=$tablename;
        $sqlfields = $_GPC["sqlfields"];
		$linkjson['sqlfields']=$sqlfields;

		$sql="select $sqlfields from ims_$tablename where id=:id ";
        $link =pdo_fetch($sql,array(":id"=> safe_gpc_string($_GPC["linkid"])));
        if(false===$link){
            return $this->getErrorMessage("商品已下架");
        }

        if(empty(safe_gpc_int($link['stock']))){
            return $this->getErrorMessage("库存不足");
        }


        $cart['dashboard_id']=$dashboardid;
        $cart['user_id']=$uid;
        $cart['link_id']=safe_gpc_string($_GPC["linkid"]);
        $cart['form_id']=safe_gpc_string($_GPC["formid"]);
        $cart['status']=1;
        $result = pdo_get(getTableName("cart"),$cart,'id');


        if(false===$result){
            $cart['link_json']=json_encode($linkjson);
            $cart['page_id']=safe_gpc_string($_GPC["page_id"]);
            $cart['page_name']=safe_gpc_string($_GPC["page_name"]);
            $cart['link_title']=safe_gpc_string($link["title"]);
            $cart['link_img']=safe_gpc_string($link["img"]);
            $cart['link_price']=safe_gpc_string($link["price"]);
            $cart['link_total']=1;
            if(empty($cart['link_price'])){
                $cart['link_price']=1;
            }
            $cart['status']=1;
            $cart['create_time']=date("Y-m-d H:i:s", time());
            $cart['update_time']=date("Y-m-d H:i:s", time());
            $result = pdo_insert(getTableName("cart"),$cart);
            if(false!==$result){
                return $this->getSuccessMessage("加入购物车成功");
            }else{
                return $this->getErrorMessage("加入购物车失败");
            }
        }else{
            return $this->getSuccessMessage("购物车已经存在此商品");
        }
    }

    public function data()
    {
        global $_GPC, $_W;
        $dashboardid = $_GPC["dashboardid"];
        $uid =$this->getUid();
        $ids= $_GPC["ids"];
        if($ids){
            $list = pdo_getall(getTableName("CART"),array('id'=>explode(",",$ids)));
        }else{
            $list = pdo_getall(getTableName("CART"),array('dashboard_id'=>$dashboardid,'user_id'=>$uid));
        }
        return $this->getPageMessage("加载数据成功",$list,count($list));
    }

    public function changeCount()
    {
        global $_GPC, $_W;
        $linkTotal = safe_gpc_int($_GPC['linktotal']);
        if($linkTotal<1){
            return $this->getErrorPage("数量不能小于1");
        }
        $id =safe_gpc_int($_GPC['id']);
        $result = pdo_update(getTableName("CART"),array("link_total"=>$linkTotal),array("id"=>$id));
        if(false!==$result){
            return $this->getSuccessMessage("修改数量成功");
        }else{
            return $this->getSuccessMessage("修改数量失败");
        }
    }



    public function delete()
    {
        global $_GPC, $_W;
        $ids    =   safe_gpc_array($_GPC['values']);
        if(!is_array($ids)){
            $ids[]    =  safe_gpc_string($_GPC['id']);
        }
        $result = pdo_delete('CART',array("id"=>$ids));
        if(false!==$result){
            return $this->getSuccessMessage("删除成功");
        }else{
            return $this->getSuccessMessage("删除失败");
        }
    }

    public function clear()
    {
        global $_GPC, $_W;
        $dashboardid = $_GPC["dashboardid"];
        $uid =$this->getUid();
        $result = pdo_delete(getTableName('CART'),array("dashboard_id"=>$dashboardid,"user_id"=>$uid));
        if(false!==$result){
            return $this->getSuccessMessage("清空成功");
        }else{
            return $this->getSuccessMessage("清空失败");
        }
    }

}